package com.xpgk.mvc.statistic.customerProductPurchaseAndSaleStatisic;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.template.expr.ast.Index;
import com.xpgk.constant.ConstantSplitPage;
import com.xpgk.dto.SplitPage;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.dept.Department;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.productSet.Product;
import com.xpgk.mvc.statistic.customerInfo.CustomerInfoStatisitSQL;
import com.xpgk.mvc.statistic.customerInfo.CustomerInfoStatisticXLS;
import com.xpgk.mvc.statistic.stockHandoverStatistic.StockHandoverStatisticSQL;
import com.xpgk.mvc.statistic.unusedProductCustomersStatistic.UnusedProductCustomersStatisticSQL;
import com.xpgk.mvc.user.User;
import com.xpgk.tools.ToolDateTime;

public class CustomerProductPurchaseAndSaleStatisicController extends BaseController{

	public void index(){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("column", Product.column_useIds);
		String sql = getSqlByBeetl(Product.sqlId_column, param);
		List<Product> products = Product.dao.find(sql, Code.cpytFw_ids);
		List<Code> parts = Code.cacheGetChild(Code.fwbw_ids);
		setAttr("products", products);
		setAttr("parts", parts);
		setAttr("startTime", ToolDateTime.getMonthOneDay(new Date()));
		render("customerProductPurchaseAndSaleStatisic.html");
	}
	/**
	 * 顾客列表
	 */
	public void view(){
		String startTime = getPara("startTime", ToolDateTime.getMonthOneDay(new Date()));
		String organIds = getPara("organIds", getCUserOrganIds());
		String endTime = getPara("endTime", ToolDateTime.getMonthOneDay(new Date()));
		String lastStartTime = getPara("lastStartTime", getCUserOrganIds());
		String lastEndTime = getPara("lastEndTime", ToolDateTime.getMonthOneDay(new Date()));
		String buyStartTime = getPara("buyStartTime", getCUserOrganIds());
		String buyEndTime = getPara("buyEndTime", getCUserOrganIds());
		
		List<Record> result = new ArrayList<>();
		
				
				String sql_product_in = getSqlMy(CustomerProductPurchaseAndSaleStatisicSQL.sqlId_buy);
				List<Record> in_sum = Db.find(sql_product_in,startTime,endTime,organIds,lastStartTime,lastEndTime,buyStartTime,buyEndTime,startTime,endTime,organIds,lastStartTime,lastEndTime,buyStartTime,buyEndTime,startTime,endTime,organIds,lastStartTime,lastEndTime,buyStartTime,buyEndTime,startTime,endTime,organIds,lastStartTime,lastEndTime,buyStartTime,buyEndTime);
				if(in_sum!= null){
					
					for(int i = 0; i < in_sum.size(); i++){
						Record r = new Record();
						if(in_sum.get(i).getInt("amggmsl")!=null){
							int amggm = in_sum.get(i).getInt("amggmsl");
							int amggmcs = amggm*25;
							r.set("_amggm", amggmcs);
						}
						if(in_sum.get(i).getFloat("amgsyks")!=null){
							float amgsy = in_sum.get(i).getFloat("amgsyks");
							float amgsycs = amgsy/80*25;
							r.set("_amgsy", amgsycs);
						}
						if(in_sum.get(i).getInt("jygmsl")!=null){
							int jygm = in_sum.get(i).getInt("jygmsl");
							int jygmcs = jygm*50;
							r.set("_jygm",jygmcs);
						}
						if( in_sum.get(i).getFloat("jysyks")!=null){
							float jysy = in_sum.get(i).getFloat("jysyks");
							float jysycs = jysy/80*50;
							r.set("_jysy", jysycs);
						}
						
//						r.set(product.getStr("ids")+i+ "_sy", inSum.getDouble("outgoing"));
//						r.set(product.getStr("ids")+i+"_kc", inSum.getFloat("physicalInventory"));
						r.set("customerName", in_sum.get(i).getStr("customerName"));
						r.set("zhsy", in_sum.get(i).getDate("zhsy"));
						r.set("registtime", in_sum.get(i).getStr("registtime"));
						r.set("date", in_sum.get(i).getDate("date"));
						r.set("phone", in_sum.get(i).getStr("phone"));
						r.set("weight", in_sum.get(i).getFloat("weight"));
						r.set("phone", in_sum.get(i).getStr("phone"));
						r.set("delWeight", in_sum.get(i).getFloat("delWeight"));
						r.set("std_weight", in_sum.get(i).getFloat("std_weight"));
						r.set("sjjs", in_sum.get(i).getDouble("sjjs"));
						r.set("treatment", in_sum.get(i).getInt("treatment"));
						r.set("capacities", in_sum.get(i).getFloat("capacities"));
						result.add(r);
					}
					
					
					String json = JsonKit.toJson(result);
					renderJson(json);
				}
	}
	
}
